package com.shop2cn.iapi.promotion.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.promotion.parameter.GetMultiProductPromotionPolicyReq;
import com.shop2cn.iapi.promotion.parameter.GetMultiProductPromotionPolicyResp;
import com.shop2cn.iapi.promotion.parameter.ProductDto;
import com.shop2cn.iapi.promotion.parameter.ProductPromotionPolicyDto;
import com.shop2cn.iapi.promotion.parameter.SellerProductDto;
import com.shop2cn.iapi.promotion.serivce.GetPromotionByProductIdsCall;
import com.shop2cn.iapi.promotion.serivce.PromotionInfoVerify;
import com.shop2cn.iapi.sellerpromotion.parameter.CreatePromotionBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JSellerPromotionCallService;
import com.ymttest.business.service.Shop2cnSellerPromotionCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;
/**
*根据买手商品Id集合获取商品是否有促销信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetPromotionByProductIds{
	private static GetMultiProductPromotionPolicyReq getpromotionbyproductidsBean;
	private static GetPromotionByProductIdsCall getpromotionbyproductidsCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据买手商品Id集合获取商品是否有促销信息");
	}

	@Before
	public void caseUp() {
		getpromotionbyproductidsBean=new GetMultiProductPromotionPolicyReq();
		getpromotionbyproductidsCall=new GetPromotionByProductIdsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getpromotionbyproductidsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionByProductIds_001() {
		Logger.start(true,"根据买手商品Id集合获取商品是否有促销信息");
		try {
			Integer sellerid = GodmchManageCallService.CreateSellerMch();
			String pid =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(sellerid, 4);
			createpromotionBean.setProductIdList(Arrays.asList(pid));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			
			Thread.sleep(1000);
			
			SellerProductDto sellerProduct = new SellerProductDto();
			
			sellerProduct.setSellerId(sellerid);
			
			List<ProductDto> productInfos = new ArrayList<ProductDto>();
			
			for (String productId : createpromotionBean.getProductIdList()) {
				ProductDto product2 = new ProductDto();
				product2.setProductId(productId);
				product2.setCurrency("CNY");
				productInfos.add(product2);
			}
				
			sellerProduct.setProductDtos(productInfos);
			
			List<SellerProductDto> sellerProductInfos = new ArrayList<SellerProductDto>();
			
			sellerProductInfos.add(sellerProduct);
			
			getpromotionbyproductidsBean.setShowPromotionDetail(true);
			getpromotionbyproductidsBean.setSellerProductDtos(sellerProductInfos);
			
			getpromotionbyproductidsCall.setData(getpromotionbyproductidsBean);
			getpromotionbyproductidsCall.callService();			
			
			Logger.verifyEquals("200", getpromotionbyproductidsCall.getCode(), "验证返回Code");
			 GetMultiProductPromotionPolicyResp resp = getpromotionbyproductidsCall.getData();

			// 验证PromotionInfo
			List<String> productIds =createpromotionBean.getProductIdList();
			for (ProductPromotionPolicyDto sellerProduct2 : resp.getProductPromotionPolicyDtos()) {
				
				Logger.verifyEquals(sellerid, sellerProduct2.getSellerId(), "验证返回SellerId");
				Logger.verifyEquals(true, sellerProduct2.isJoinPromotion(), "验证返回IsPromotionProduct");

				PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
				promotionInfoVerify.verifyPromotionPolicyInDB(sellerProduct2.getPromotionPolicyDto());
				promotionInfoVerify.isStringInList(productIds,sellerProduct2.getProductId());
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionByProductIds_002() {
		Logger.start(true,"根据买手商品Id集合获取商品是否有促销信息-活动已取消");
		try {
			Integer sellerid = GodmchManageCallService.CreateSellerMch();
			String pid =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(sellerid, 4);
			createpromotionBean.setProductIdList(Arrays.asList(pid));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			Shop2cnSellerPromotionCallService.EndAllEnablePromotion(sellerid);
			Thread.sleep(1000);

			SellerProductDto sellerProduct = new SellerProductDto();
			
			sellerProduct.setSellerId(sellerid);
			
			List<ProductDto> productInfos = new ArrayList<ProductDto>();
			
			for (String productId :createpromotionBean.getProductIdList()) {
				ProductDto product2 = new ProductDto();
				product2.setProductId(productId);
				product2.setCurrency("CNY");
				productInfos.add(product2);
			}
				
			sellerProduct.setProductDtos(productInfos);
			
			List<SellerProductDto> sellerProductInfos = new ArrayList<SellerProductDto>();
			
			sellerProductInfos.add(sellerProduct);
			
			getpromotionbyproductidsBean.setShowPromotionDetail(true);
			getpromotionbyproductidsBean.setSellerProductDtos(sellerProductInfos);
			
			getpromotionbyproductidsCall.setData(getpromotionbyproductidsBean);
			getpromotionbyproductidsCall.callService();			
			
			Logger.verifyEquals("200", getpromotionbyproductidsCall.getCode(), "验证返回Code");
			GetMultiProductPromotionPolicyResp resp = getpromotionbyproductidsCall.getData();
			
			Logger.verifyEquals(0, resp.getProductPromotionPolicyDtos().size(), "验证返回ProductsPromotion条数为0");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionByProductIds_003() {
		Logger.start(true,"根据买手商品Id集合获取商品是否有促销信息-活动未开始");
		try {
			Integer sellerid = GodmchManageCallService.CreateSellerMch();
			String pid =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(sellerid, 4);
			createpromotionBean.setBegin(YMTDateUtil.getBeforeOrNextDay(1,"yyyy-MM-dd HH:mm:ss"));
			createpromotionBean.setEnd(YMTDateUtil.getBeforeOrNextDay(2,"yyyy-MM-dd HH:mm:ss"));
			
			createpromotionBean.setProductIdList(Arrays.asList(pid));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			
			Thread.sleep(1000);

			SellerProductDto sellerProduct = new SellerProductDto();
			
			sellerProduct.setSellerId(sellerid);
			
			List<ProductDto> productInfos = new ArrayList<ProductDto>();
			
			for (String productId :createpromotionBean.getProductIdList()) {
				ProductDto product2 = new ProductDto();
				product2.setProductId(productId);
				product2.setCurrency("CNY");
				productInfos.add(product2);
			}
				
			sellerProduct.setProductDtos(productInfos);
			
			List<SellerProductDto> sellerProductInfos = new ArrayList<SellerProductDto>();
			
			sellerProductInfos.add(sellerProduct);
			
			getpromotionbyproductidsBean.setShowPromotionDetail(true);
			getpromotionbyproductidsBean.setSellerProductDtos(sellerProductInfos);
			
			getpromotionbyproductidsCall.setData(getpromotionbyproductidsBean);
			getpromotionbyproductidsCall.callService();			
			
			Logger.verifyEquals("200", getpromotionbyproductidsCall.getCode(), "验证返回Code");
			GetMultiProductPromotionPolicyResp resp = getpromotionbyproductidsCall.getData();
			
			Logger.verifyEquals(0, resp.getProductPromotionPolicyDtos().size(), "验证返回ProductsPromotion条数为0");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionByProductIds_004() {
		Logger.start(true,"根据买手商品Id集合获取商品是否有促销信息-不存在活动的商品不返回");
		try {
			Integer sellerid = GodmchManageCallService.CreateSellerMch();
			String pid =TestDataManager.getRandomUUID().toString();
			String pid2 =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(sellerid, 4);
			createpromotionBean.setProductIdList(Arrays.asList(pid));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			
			Thread.sleep(1000);

			SellerProductDto sellerProduct = new SellerProductDto();
			
			sellerProduct.setSellerId(sellerid);
			
			List<ProductDto> productInfos = new ArrayList<ProductDto>();
			
			for (String productId :createpromotionBean.getProductIdList()) {
				ProductDto product2 = new ProductDto();
				product2.setProductId(productId);
				product2.setCurrency("CNY");
				productInfos.add(product2);
			}
			
			ProductDto product2 = new ProductDto();
			product2.setProductId(pid2);
			product2.setCurrency("CNY");
			productInfos.add(product2);

			sellerProduct.setProductDtos(productInfos);
			
			List<SellerProductDto> sellerProductInfos = new ArrayList<SellerProductDto>();
			
			sellerProductInfos.add(sellerProduct);
			
			getpromotionbyproductidsBean.setShowPromotionDetail(true);
			getpromotionbyproductidsBean.setSellerProductDtos(sellerProductInfos);
			getpromotionbyproductidsCall.setData(getpromotionbyproductidsBean);
			getpromotionbyproductidsCall.callService();			
			
			Logger.verifyEquals("200", getpromotionbyproductidsCall.getCode(), "验证返回Code");
			GetMultiProductPromotionPolicyResp resp = getpromotionbyproductidsCall.getData();

			// 验证PromotionInfo
			List<String> productIds =createpromotionBean.getProductIdList();
			for (ProductPromotionPolicyDto sellerProduct2 : resp.getProductPromotionPolicyDtos()) {
				
				Logger.verifyEquals(sellerid, sellerProduct2.getSellerId(), "验证返回SellerId");
				Logger.verifyEquals(true, sellerProduct2.isJoinPromotion(), "验证返回IsPromotionProduct");

				PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
				promotionInfoVerify.verifyPromotionPolicyInDB(sellerProduct2.getPromotionPolicyDto());
				
				Logger.verifyEquals(true, promotionInfoVerify.isStringInList(productIds,sellerProduct2.getProductId()), "验证返回IsPromotionProduct");

				Logger.verifyEquals(false, sellerProduct2.getProductId().equals(pid2), "验证返回删除的productId不会返回");

			}
			Logger.verifyEquals(1, resp.getProductPromotionPolicyDtos().size(), "验证返回ProductsPromotion条数为1");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionByProductIds_006() {
		Logger.start(true,"根据买手商品Id集合获取商品是否有促销信息-SellerId空");
		try {
			Integer sellerid = GodmchManageCallService.CreateSellerMch();
			String pid =TestDataManager.getRandomUUID().toString();
			String pid2 =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(sellerid, 4);
			createpromotionBean.setProductIdList(Arrays.asList(pid));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			
			Thread.sleep(1000);

			
			SellerProductDto sellerProduct = new SellerProductDto();
			
//			sellerProduct.setSellerId(sellerid);
			
			List<ProductDto> productInfos = new ArrayList<ProductDto>();
			
			for (String productId :createpromotionBean.getProductIdList()) {
				ProductDto product2 = new ProductDto();
				product2.setProductId(productId);
				product2.setCurrency("CNY");
				productInfos.add(product2);
			}
				
			sellerProduct.setProductDtos(productInfos);
			
			List<SellerProductDto> sellerProductInfos = new ArrayList<SellerProductDto>();
			
			sellerProductInfos.add(sellerProduct);
			
			getpromotionbyproductidsBean.setShowPromotionDetail(true);
			getpromotionbyproductidsBean.setSellerProductDtos(sellerProductInfos);
			
			getpromotionbyproductidsCall.setData(getpromotionbyproductidsBean);
			getpromotionbyproductidsCall.callService();			
			
			Logger.verifyEquals("201", getpromotionbyproductidsCall.getCode(), "验证返回Code");
			Logger.verifyEquals("请求处理失败|sellerId is <= 0", getpromotionbyproductidsCall.getMsg(), "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionByProductIds_007() {
		Logger.start(true,"根据买手商品Id集合获取商品是否有促销信息-product空");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));			
			SellerProductDto sellerProduct = new SellerProductDto();
			
			sellerProduct.setSellerId(sellerid);
			
			List<ProductDto> productInfos = new ArrayList<ProductDto>();
			
			sellerProduct.setProductDtos(productInfos);
			
			List<SellerProductDto> sellerProductInfos = new ArrayList<SellerProductDto>();
			
			sellerProductInfos.add(sellerProduct);
			
			getpromotionbyproductidsBean.setShowPromotionDetail(true);
			getpromotionbyproductidsBean.setSellerProductDtos(sellerProductInfos);
			
			getpromotionbyproductidsCall.setData(getpromotionbyproductidsBean);
			getpromotionbyproductidsCall.callService();			
			
			Logger.verifyEquals("201", getpromotionbyproductidsCall.getCode(), "验证返回Code");
			Logger.verifyEquals("请求处理失败|seller products  is empty, sellerId:"+sellerid, getpromotionbyproductidsCall.getMsg(), "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionByProductIds_008() {
		Logger.start(true,"根据买手商品Id集合获取商品是否有促销信息-Seller与productid不匹配");
		try {
			Integer sellerid = GodmchManageCallService.CreateSellerMch();
			String pid =TestDataManager.getRandomUUID().toString();
			String pid2 =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(sellerid, 4);
			createpromotionBean.setProductIdList(Arrays.asList(pid));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			
			Thread.sleep(1000);

			SellerProductDto sellerProduct = new SellerProductDto();
			
			sellerProduct.setSellerId(3383);
			
			List<ProductDto> productInfos = new ArrayList<ProductDto>();
			
			for (String productId :createpromotionBean.getProductIdList()) {
				ProductDto product2 = new ProductDto();
				product2.setProductId(productId);
				product2.setCurrency("CNY");
				productInfos.add(product2);
			}
				
			sellerProduct.setProductDtos(productInfos);
			
			List<SellerProductDto> sellerProductInfos = new ArrayList<SellerProductDto>();
			
			sellerProductInfos.add(sellerProduct);
			
			getpromotionbyproductidsBean.setShowPromotionDetail(true);
			getpromotionbyproductidsBean.setSellerProductDtos(sellerProductInfos);
			
			getpromotionbyproductidsCall.setData(getpromotionbyproductidsBean);
			getpromotionbyproductidsCall.callService();			
			
			Logger.verifyEquals("200", getpromotionbyproductidsCall.getCode(), "验证返回Code");
			GetMultiProductPromotionPolicyResp resp = getpromotionbyproductidsCall.getData();
			
			Logger.verifyEquals(0, resp.getProductPromotionPolicyDtos().size(), "验证返回ProductsPromotion条数为0");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionByProductIds_009() {
		Logger.start(true,"根据买手商品Id集合获取商品是否有促销信息-JPY");
		try {
			Integer sellerid = GodmchManageCallService.CreateCountrySellerMch("JPN");
			String pid =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(sellerid, 4);
			createpromotionBean.setProductIdList(Arrays.asList(pid));
			createpromotionBean.setCurrency("JPY");
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			
			Thread.sleep(1000);
			
			SellerProductDto sellerProduct = new SellerProductDto();
			
			sellerProduct.setSellerId(sellerid);
			
			List<ProductDto> productInfos = new ArrayList<ProductDto>();
			
			for (String productId : createpromotionBean.getProductIdList()) {
				ProductDto product2 = new ProductDto();
				product2.setProductId(productId);
				product2.setCurrency("JPY");
				productInfos.add(product2);
			}
				
			sellerProduct.setProductDtos(productInfos);
			
			List<SellerProductDto> sellerProductInfos = new ArrayList<SellerProductDto>();
			
			sellerProductInfos.add(sellerProduct);
			
			getpromotionbyproductidsBean.setShowPromotionDetail(true);
			getpromotionbyproductidsBean.setSellerProductDtos(sellerProductInfos);
			
			getpromotionbyproductidsCall.setData(getpromotionbyproductidsBean);
			getpromotionbyproductidsCall.callService();			
			
			Logger.verifyEquals("200", getpromotionbyproductidsCall.getCode(), "验证返回Code");
			 GetMultiProductPromotionPolicyResp resp = getpromotionbyproductidsCall.getData();

			// 验证PromotionInfo
			List<String> productIds =createpromotionBean.getProductIdList();
			for (ProductPromotionPolicyDto sellerProduct2 : resp.getProductPromotionPolicyDtos()) {
				
				Logger.verifyEquals(sellerid, sellerProduct2.getSellerId(), "验证返回SellerId");
				Logger.verifyEquals(true, sellerProduct2.isJoinPromotion(), "验证返回IsPromotionProduct");

				PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
				promotionInfoVerify.verifyPromotionPolicyInDB(sellerProduct2.getPromotionPolicyDto());
				promotionInfoVerify.isStringInList(productIds,sellerProduct2.getProductId());
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}